#ifndef __TRANSPORTLAYER_H__
#define __TRANSPORTLAYER_H__

#include "DR_define.h"
#include "FFMatcher.h"
#include "TransferredPKG.h"
#include "ACKPkg.h"
#include "pb.h"
#include "ProtobufPkg.h"

//新数据包接收事件,void*为观察者(由SetObserver设置)
typedef void (TransportLayer_NewDataHandler)(TransferredPKG, void*);


//ACK事件,void*为观察者(由SetObserver设置)
typedef void (TransportLayer_NewAckHandler)(ACKPkg, void*);


//需要发送数据事件,void*为观察者(由SetObserver设置)
typedef void (TransportLayer_SendDataNeed)(unsigned char*, int, void*);






/*
数据包传输层
sendData()自动封包为TransferredPkg，包ID自动增长
自动回发ACK
*/
class TransportLayer
{
public:
	//原始数据传入（由硬件层使用）
	void inStream(unsigned char* buf, int len);

	//原始数据传入（由硬件层使用）
	void inStream(unsigned char b);

	//发送数据。会自动封包为TransferredPkg
	void sendData(unsigned char* data, int len, bool useCRC);


	static bool isAckPkg(unsigned char* pkg, int len);


public:
	TransportLayer();

private:
	FFMatcher ffMatcher;

public:
	//监听新数据包接收事件
	void setNewDataPKGEvent(TransportLayer_NewDataHandler* handler);

	//监听新ack接收事件
	void setAckEvent(TransportLayer_NewAckHandler* handler);

	//监听需要发送数据事件
	void setSendDataNeedEvent(TransportLayer_SendDataNeed* handler);

	//设置事件观察者
	void SetEventObserver(void* observer);

	TransportLayer_NewDataHandler* newDataHandler;

	TransportLayer_NewAckHandler* newAckHandler;

	TransportLayer_SendDataNeed* dataSendHandler;




	//直接发送数据（不封包）
	void sendBuffer(unsigned char* buf, int len);

	//事件监听观察者
	void* event_observer;

private:


	//下一个包ID
	int nextPkgID;

};


#endif // !__TRANSPORTLAYER_H__
